﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace BooksStore
{
    class PaySystemImpl
    {
        public void increaseProfit(int cash) 
        {
            if (cash > 0) 
            {
                long startTime = currentTimeMillis();
                double dollars = cash / 31;
                Console.Out.WriteLine("Summa v dollarah " + dbl2str(dollars));
                String sql = buildInsertString(dollars);
                MySqlConnection connection = null;
                try 
                {
                    connection = getConnection();
                } catch (Exception e) {
                    Console.Out.WriteLine(e.Message);
                    Console.Out.WriteLine(e.StackTrace);
                }
                try 
                {
                    MySqlCommand command = new MySqlCommand(sql, connection);
                    command.ExecuteNonQuery();
                } catch (Exception e) {
                    Console.Out.WriteLine(e.Message);
                    Console.Out.WriteLine(e.StackTrace);
                }
                long endTime = currentTimeMillis();
                Console.Out.WriteLine(getSecondsToTimeFormat(startTime, endTime));
            }
        }

        private static readonly DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        private long currentTimeMillis()
        {
            return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
        }

        private MySqlConnection getConnection()
        {
            string ConnectionString = "Database=bookstore;Data Source=localhost;User Id=user;Password=password";
            var connection = new MySqlConnection(ConnectionString);
            connection.Open();
            return connection;
        }

        protected String buildInsertString(double dollars)
        {
            String sql = (new StringBuilder("insert into ")).Append("cash").Append(" (").ToString();
            sql += (new StringBuilder(sql)).Append("year,version_num,operatins,summ").ToString();
            sql += (new StringBuilder(sql)).Append(") values ('").ToString();
            sql += (new StringBuilder(sql)).Append("2012").Append("',").ToString();
            sql += (new StringBuilder(sql)).Append("325435").Append("',").ToString();
            sql += (new StringBuilder(sql)).Append("cash").Append("',").ToString();
            sql += (new StringBuilder(sql)).Append(dbl2str(dollars)).Append("',").ToString();
            sql += (new StringBuilder(sql)).Append("0,'Clarity','','')").ToString();
            return sql;
        }

        private static String dbl2str(Object obj)
        {
            string value;
            string str = obj.ToString();
            if (str.IndexOf('E') != -1)
            {
                //System.out.print("1  ");
                string[] qwe = str.Split('E');
                string newstr = "";
                int step = int.Parse(qwe[1]);
                for (int ind = 0; ind < qwe[0].Length; ind++)
                {
                    if (step > 0)
                    {
                        if ((ind == (step + 2)) && (ind != qwe[0].Length))
                        {
                            newstr = newstr + '.';
                        }

                        if (str[ind] != '.')
                        {
                            newstr = newstr + str[ind];
                        }
                        if ((ind == qwe[0].Length - 1) && (ind <= step))
                        {
                            for (int ind2 = 0; ind2 < (step + 1 - ind); ind2++)
                            {
                                newstr = newstr + "0";
                            }
                        }
                    }
                    else
                    {
                        if (ind == 0)
                        {
                            newstr = newstr + "0.";
                            for (int ind3 = 0; ind3 > step + 1; ind3--)
                            {
                                newstr = newstr + "0";
                            }
                        }
                        if (str[ind] != '.')
                        {
                            newstr = newstr + str[ind];
                        }
                    }
                }
                value = newstr;
            }
            else
            {
                value = obj.ToString();
            }
            return value;
        }


        private String getSecondsToTimeFormat(long startTime, long finishTime)
        {

            int secs = (int)Math.Round((double)(finishTime - startTime) / 1000);

            int hours = secs / 3600,
                    remainder = secs % 3600,
                    minutes = remainder / 60,
                    seconds = remainder % 60;

            StringBuilder result = new StringBuilder();

            if (hours > 0)
            {
                result.Append((hours < 10 ? "0" : "") + hours).Append(":");
            }

            if (minutes > 0 || hours > 0)
            {
                result.Append((minutes < 10 ? "0" : "") + minutes).Append(":");
            }

            if (seconds > 0 || hours > 0 || minutes > 0)
            {
                result.Append((seconds < 10 ? "0" : "") + seconds);
            }

            if (hours == 0 && minutes == 0)
            {
                if (seconds == 1)
                {
                    result.Append(" second");
                }
                else
                {
                    result.Append(" seconds");
                }
            }

            return result.ToString();
        }
    }
}
